﻿<%@ WebHandler Language="C#" Class="ApsPlanInfoPreReport" %>

using System;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using ReportBusiness;
using System.Collections;
using System.IO;

/**
 * Description: 预排转工计划报表
 * Copyright (c): 通力凯顿（北京）系统集成有限公司
 * Author: zhunan
 * Date: 2025.1.8
 */

public class ApsPlanInfoPreReport : IHttpHandler
{
    uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                Result r = new Result();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getData":
                        r = getData(jObject);//查询
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "exportExcel":
                        getExportExcel(jObject);//导出
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    //查询公用sql
    string getQuerySql(JObject jObject)
    {
        string factoryid = "", workcenterid = "", teamid = "";
        if (jObject["selectOrg"] != null && jObject["selectOrg"].ToString() != "[]")
        {
            JArray selectOrg = JArray.Parse(jObject["selectOrg"].ToString());
            if (selectOrg.Count == 3)
            {
                factoryid = selectOrg[0].ToString();
                workcenterid = selectOrg[1].ToString();
                teamid = selectOrg[2].ToString();
            }
            else if (selectOrg.Count == 2)
            {
                factoryid = selectOrg[0].ToString();
                workcenterid = selectOrg[1].ToString();
            }
            else
            {
                factoryid = selectOrg[0].ToString();
            }
        }

        string sql = @"
SELECT cci.mfgordername,cci.containername,cci.productname,cci.productdescription,
       wfb.workflowname || ':' || wf.workflowrevision workflowinfo,cci.qty,
       DECODE(cci.containertype,2,'数量批',3,'子序号批',7,'单价单卡') containertype,apsd.workflowstepname,
       f.factoryname,w.workcentername,te.teamname,f2.factoryname specfactoryname,
       DECODE(aps.apstype,1,'正排',2,'倒排',3,'瓶颈排产','其他') apstype,aps.createdate,aps.starttime,aps.endtime
FROM apsplaninfopre aps
LEFT JOIN apsplandetailpre apsd ON apsd.apsplaninfoid = aps.apsplaninfoid
LEFT JOIN containercurrentinfo cci ON cci.containerid = aps.containerid
LEFT JOIN workflow wf ON wf.workflowid = aps.workflowid
LEFT JOIN workflowbase wfb ON wfb.workflowbaseid = wf.workflowbaseid
LEFT JOIN factory f ON f.factoryid = cci.originalfactoryid
LEFT JOIN factory f2 ON f2.factoryid = apsd.specfactoryid
LEFT JOIN productteaminfo pm ON pm.productname = cci.productname
LEFT JOIN team te ON te.teamid = pm.teamid
LEFT JOIN workcenter w ON w.workcenterid = te.workcenterid
WHERE aps.isuse = 0 AND apsd.isuse = 0 AND apsd.specfactoryid <> cci.originalfactoryid";
        //分厂
        if (jObject["factoryid"] != null && jObject["factoryid"].ToString() != "")
        {
            sql += string.Format(" AND cci.originalfactoryid = '{0}'", jObject["factoryid"].ToString());
        }

        //订单
        if (jObject["mfgordername"] != null && jObject["mfgordername"].ToString() != "")
        {
            sql += string.Format(" AND cci.mfgordername = '{0}'", jObject["mfgordername"].ToString());
        }

        //件号
        if (jObject["productname"] != null && jObject["productname"].ToString() != "")
        {
            sql += string.Format(" AND cci.productname = '{0}'", jObject["productname"].ToString());
        }

        //流水卡号
        if (jObject["containername"] != null && jObject["containername"].ToString() != "")
        {
            sql += string.Format(" AND cci.containername = '{0}'", jObject["containername"].ToString());
        }

        //工序
        if (jObject["workflowstepname"] != null && jObject["workflowstepname"].ToString() != "")
        {
            sql += string.Format(" AND apsd.workflowstepname LIKE '%{0}%'", jObject["workflowstepname"].ToString());
        }

        //工区
        if (workcenterid != "")
        {
            sql += string.Format(" AND w.workcenterid = '{0}'", workcenterid);
        }

        //班组
        if (teamid != "")
        {
            sql += string.Format(" AND te.teamid = '{0}'", teamid);
        }
        return sql;
    }

    Result getData(JObject jObject)
    {
        string sql = getQuerySql(jObject);
        uMESPagingDataDTO dto = dal.GetPagingDataIns(sql, (int)jObject["currentPage"], (int)jObject["pageSize"]);
        DataTable dtResult = dto.DBTable;
        foreach (DataColumn col in dtResult.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        Result r = new Result();
        r.data = dtResult;
        r.total = Convert.ToInt32(dto.RowCount);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //导出
    void getExportExcel(JObject jObject)
    {
        string sql = getQuerySql(jObject);
        DataTable dtResult = dal.GetDataTable(sql);
        foreach (DataColumn col in dtResult.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        dtResult.Columns["mfgordername"].Caption = "订单号";
        dtResult.Columns["containername"].Caption = "批次号";
        dtResult.Columns["productname"].Caption = "零件号";
        dtResult.Columns["productdescription"].Caption = "零件名称";
        dtResult.Columns["workflowinfo"].Caption = "工艺规程";
        dtResult.Columns["qty"].Caption = "在制数";
        dtResult.Columns["containertype"].Caption = "批次类型";
        dtResult.Columns["workflowstepname"].Caption = "转工序";
        dtResult.Columns["factoryname"].Caption = "主制分厂";
        dtResult.Columns["workcentername"].Caption = "主制工区";
        dtResult.Columns["teamname"].Caption = "主制单元";
        dtResult.Columns["specfactoryname"].Caption = "承制分厂";
        dtResult.Columns["apstype"].Caption = "排产策略";
        dtResult.Columns["createdate"].Caption = "计划生产时间";
        dtResult.Columns["starttime"].Caption = "计划开始时间";
        dtResult.Columns["endtime"].Caption = "计划结束时间";

        ExportHelper.ExportExcel(dtResult, "预排转工计划报表导出数据");
    }
}